package web

import (
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	"net/http"
	"testPaper/common"
	"testPaper/model"
	"testPaper/response"
)

// CreateCase 创建案例
func CreateCase(c *gin.Context) {
	DB := common.GetDB()
	paperId := c.PostForm("paperId")
	subject := c.PostForm("subject")
	img := c.PostForm("img")
	adminPhone := c.PostForm("adminPhone")

	if len(paperId) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "试卷id不能为空")
		return
	} else if len(subject) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "标题不能为空")
		return
	}
	caseId := uuid.NewV1().String()
	if CaseExits(DB, paperId) {
		newCase := model.Case{
			Subject:    subject,
			Img:        img,
			AdminPhone: adminPhone,
			PaperId:    paperId,
			CaseId:     caseId,
		}
		DB.Create(&newCase)
		response.Reponse(c, http.StatusOK, 200, nil, "创建成功")
		return
	} else {
		response.Reponse(c, http.StatusOK, 422, nil, "传入的试卷Id不存在")
		return
	}
}
